/*
    ** 控制组件
    ** 用于次序化管理硬件及通讯时序
*/

#ifndef DEVICECONTROL_H
#define DEVICECONTROL_H

#include "src/define/func_define.h"
#include <tuple>
#include <deque>
#include "MU_API.h"
#include "src/device/Robot/robothelper.h"
#include "src/device/Camera/camerahelper.h"
#include "src/device/Laser/laserhelper.h"
#include "src/device/Machine/MachineHelper.h"
#include "src/device/PLC/PLCHelper.h"
#include "src/DataProvider/SQL/sqlProvider.h"
#include "src/SoftWare/Log/LogHelper.h"

#include <future>
#include <functional>

static MU_RETURNS handler_error(MU_E_TYPE e){
    /// 此处终止当前对象
    return MU_RETURN_SUCCESS;
}

/**
 * @brief The ConfApp struct
 * @details 从配置文件中读取是否需要初始化
 */
struct ConfApp{
    bool 	log ;
    bool 	robot ;
    bool 	cam   ;
    bool 	plc	;
    bool 	machine ;
    bool 	laser	;
    bool 	sql	;
    bool 	image	;
    bool 	io	;
    bool 	weld	;

    void init(){
        Fast::config app("etc/App/app.sys");

        io = app.get<bool>("init.io");
        log = app.get<bool>("init.log");
        robot = app.get<bool>("init.robot");
        cam = app.get<bool>("init.cam");
        plc = app.get<bool>("init.plc");
        machine = app.get<bool>("init.machine");
        laser = app.get<bool>("init.laser");
        sql = app.get<bool>("init.sql");
        image = app.get<bool>("init.image");
        weld = app.get<bool>("init.weld");
    }
    void inited(){
        Fast::config app("etc/App/app.sys");

        io = app.get<bool>("inited.io");
        log = app.get<bool>("inited.log");
        robot = app.get<bool>("inited.robot");
        cam = app.get<bool>("inited.cam");
        plc = app.get<bool>("inited.plc");
        machine = app.get<bool>("inited.machine");
        laser = app.get<bool>("inited.laser");
        sql = app.get<bool>("inited.sql");
        image = app.get<bool>("inited.image");
        weld = app.get<bool>("inited.weld");
    }
};

static void handler_exception()
{
    std::packaged_task<MU_RETURNS(MU_E_TYPE)> task(handler_error) ;
    std::future<MU_RETURNS> result = task.get_future();
    // task(2, 3); //2*3=6
    std::cout << "task_lambda:2*3=" << result.get() << std::endl;
}


class DeviceControl
{
public:
    DeviceControl();
    static DeviceControl* GetInstance(){
        static QMutex mutex;
        static QScopedPointer<DeviceControl> inst;
        if (Q_UNLIKELY(!inst)) {
            mutex.lock();
            if (!inst) {
                inst.reset(new DeviceControl);
            }
            mutex.unlock();
        }
        return inst.data();
    }

    /**
     * @brief DeviceControl::initialDevice
     * @return MU_RETURNS
     * @date 2020-04-06
     * @details
     * 初始化系统的硬件部分
     * @author zxx
     */
    MU_RETURNS initialDevice();     /// 初始化硬件信息


private:
    ConfApp initing;        // 初始化之前的，检测哪个模块需要初始化
    ConfApp inited;        // 初始化之后的
    int  startType = 0;  // 0.默认,启动一次   1.如果没启动成功就一直等待

    void initNoUse();
    MU_RETURNS initSystem();        /// 初始化系统级功能

    /**
     * @brief DeviceControl::initMember
     * @attention
     * 初始化成员变量，主要针对于系统初始化的状态
     */
    void initMember();

    /**
     * @brief printDeviceList
     * @details 打印当前的设备初始化结果
     */
    void printDeviceList();

    /**
     * @brief DeviceControl::reflushData
     * @anchor 张xx
     * @version 0.1
     * @details 获取安全点
     */
    void reflushData();             // 从数据库刷新
};

#endif // DEVICECONTROL_H
